-- ============================================
-- ROCKET CASINO - MACHINE A SOUS
-- Le COMPUTER est le jeu : connexion au clavier physique, puis jeu
-- entierement a la souris (clics), avec une interface visuelle
-- inspiree d'une machine a sous classique dans un style Team Rocket.
-- Le MONITOR n'affiche QUE la Poke Ball Team Rocket qui tourne, en
-- boucle, jamais touchee, jamais utilisee pour jouer.
--
-- Fichier requis dans le meme dossier : client_solde.lua
-- ============================================

local bank = dofile("client_solde.lua")
math.randomseed(os.epoch and os.epoch("utc") or os.time())

local BET_MIN, BET_MAX, BET_STEP = 1, 20, 1

-- ============================================
-- PARTIE 1 : POKE BALL DECORATIVE SUR LE MONITOR
-- ============================================

local function buildBannerLoop()
    local mon = peripheral.find("monitor")
    if not mon then
        return nil -- pas de moniteur branche : on continue sans deco
    end

    mon.setTextScale(0.5)
    mon.setBackgroundColor(colors.black)
    mon.clear()
    local mw, mh = mon.getSize()

    local FRAME_DELAY = 0.1
    local ROTATION_SPEED = 0.22

    local function newCanvas(bg)
        local c = {}
        for y = 1, mh do
            c[y] = {}
            for x = 1, mw do
                c[y][x] = { bg = bg, ch = " ", fg = colors.white }
            end
        end
        return c
    end

    local function px(canvas, x, y, bg, ch, fg)
        x, y = math.floor(x + 0.5), math.floor(y + 0.5)
        if x >= 1 and x <= mw and y >= 1 and y <= mh then
            local cell = canvas[y][x]
            if bg then cell.bg = bg end
            if ch then
                cell.ch = ch
                cell.fg = fg or cell.fg
            end
        end
    end

    local function render(canvas)
        local toBlit = colors.toBlit
        for y = 1, mh do
            local row = canvas[y]
            local chars, fgs, bgs = {}, {}, {}
            for x = 1, mw do
                local cell = row[x]
                chars[x] = cell.ch
                fgs[x] = toBlit(cell.fg)
                bgs[x] = toBlit(cell.bg)
            end
            mon.setCursorPos(1, y)
            mon.blit(table.concat(chars), table.concat(fgs), table.concat(bgs))
        end
    end

    local function fillEllipse(canvas, cx, cy, rx, ry, color)
        if rx <= 0 or ry <= 0 then return end
        for y = math.floor(cy - ry), math.ceil(cy + ry) do
            local dy = (y - cy) / ry
            local t = 1 - dy * dy
            if t >= 0 then
                local dx = rx * math.sqrt(t)
                for x = math.floor(cx - dx), math.ceil(cx + dx) do
                    px(canvas, x, y, color)
                end
            end
        end
    end

    local R_PATTERN = { "####.", "#...#", "#...#", "####.", "#..#.", "#...#", "#...#" }

    local function drawGlyphR(canvas, x0, y0, scale, color)
        for row = 1, #R_PATTERN do
            local line = R_PATTERN[row]
            for col = 1, #line do
                if line:sub(col, col) == "#" then
                    for sy = 0, scale - 1 do
                        for sx = 0, scale - 1 do
                            px(canvas, x0 + (col - 1) * scale + sx, y0 + (row - 1) * scale + sy, color)
                        end
                    end
                end
            end
        end
    end

    local function drawPokeball(canvas, cx, cy, r, angle)
        local squish = math.max(0.12, math.abs(math.cos(angle)))
        local rx = r * squish
        local ry = r

        fillEllipse(canvas, cx, cy, rx + 1.2, ry + 1.2, colors.gray)

        for y = math.floor(cy - ry), math.ceil(cy + ry) do
            local dy = (y - cy) / ry
            local t = 1 - dy * dy
            if t >= 0 then
                local dx = rx * math.sqrt(t)
                local color
                if y < cy - ry * 0.08 then
                    color = colors.black
                elseif y > cy + ry * 0.08 then
                    color = colors.red
                else
                    color = colors.lightGray
                end
                for x = math.floor(cx - dx), math.ceil(cx + dx) do
                    px(canvas, x, y, color)
                end
            end
        end

        for x = math.floor(cx - rx), math.ceil(cx + rx) do
            px(canvas, x, cy, colors.black)
        end

        local buttonR = math.max(1, r * 0.28 * squish)
        fillEllipse(canvas, cx, cy, buttonR + 0.6, buttonR + 0.6, colors.black)
        fillEllipse(canvas, cx, cy, buttonR, buttonR, colors.white)
        fillEllipse(canvas, cx, cy, buttonR * 0.5, buttonR * 0.5, colors.lightGray)

        if squish > 0.55 then
            local scale = math.max(1, math.floor(r * squish / 5))
            local rx0 = math.floor(cx - 2 * scale)
            local ry0 = math.floor(cy - ry * 0.75)
            for dx = -1, 1 do
                for dy = -1, 1 do
                    if dx ~= 0 or dy ~= 0 then
                        drawGlyphR(canvas, rx0 + dx, ry0 + dy, scale, colors.black)
                    end
                end
            end
            drawGlyphR(canvas, rx0, ry0, scale, colors.red)
        end
    end

    return function()
        local angle = 0
        local cx, cy = math.floor(mw / 2), math.floor(mh / 2)
        local r = math.floor(math.min(mw, mh) * 0.4)
        while true do
            local canvas = newCanvas(colors.black)
            drawPokeball(canvas, cx, cy, r, angle)
            render(canvas)
            angle = angle + ROTATION_SPEED
            sleep(FRAME_DELAY)
        end
    end
end

-- ============================================
-- PARTIE 2 : LE JEU, SUR L'ECRAN DU COMPUTER (clavier pour le login,
-- puis 100% a la souris)
-- ============================================

local W, H = term.getSize()

-------------------------------
-- OUTILS DE DESSIN BAS NIVEAU (canvas pixel + blit, sur le terminal)
-------------------------------

local function newCanvas(bg)
    local c = {}
    for y = 1, H do
        c[y] = {}
        for x = 1, W do
            c[y][x] = { bg = bg, ch = " ", fg = colors.white }
        end
    end
    return c
end

local function px(canvas, x, y, bg, ch, fg)
    x, y = math.floor(x + 0.5), math.floor(y + 0.5)
    if x >= 1 and x <= W and y >= 1 and y <= H then
        local cell = canvas[y][x]
        if bg then cell.bg = bg end
        if ch then
            cell.ch = ch
            cell.fg = fg or cell.fg
        end
    end
end

local function render(canvas)
    local toBlit = colors.toBlit
    for y = 1, H do
        local row = canvas[y]
        local chars, fgs, bgs = {}, {}, {}
        for x = 1, W do
            local cell = row[x]
            chars[x] = cell.ch
            fgs[x] = toBlit(cell.fg)
            bgs[x] = toBlit(cell.bg)
        end
        term.setCursorPos(1, y)
        term.blit(table.concat(chars), table.concat(fgs), table.concat(bgs))
    end
end

local function fillEllipse(canvas, cx, cy, rx, ry, color)
    if rx <= 0 or ry <= 0 then return end
    for y = math.floor(cy - ry), math.ceil(cy + ry) do
        local dy = (y - cy) / ry
        local t = 1 - dy * dy
        if t >= 0 then
            local dx = rx * math.sqrt(t)
            for x = math.floor(cx - dx), math.ceil(cx + dx) do
                px(canvas, x, y, color)
            end
        end
    end
end

local function fillRect(canvas, x1, y1, x2, y2, color)
    for y = y1, y2 do
        for x = x1, x2 do
            px(canvas, x, y, color)
        end
    end
end

local function fillTriangleUp(canvas, cx, baseY, tipY, halfWidth, color)
    local height = baseY - tipY
    if height <= 0 then return end
    for y = tipY, baseY do
        local f = (y - tipY) / height
        local w = halfWidth * f
        for x = math.floor(cx - w), math.ceil(cx + w) do
            px(canvas, x, y, color)
        end
    end
end

local function fillTriangleDown(canvas, cx, baseY, tipY, halfWidth, color)
    local height = tipY - baseY
    if height <= 0 then return end
    for y = baseY, tipY do
        local f = (tipY - y) / height
        local w = halfWidth * f
        for x = math.floor(cx - w), math.ceil(cx + w) do
            px(canvas, x, y, color)
        end
    end
end

local function fillTriangleSide(canvas, baseX, tipX, cy, halfHeight, color)
    local width = tipX - baseX
    if width == 0 then return end
    local step = width > 0 and 1 or -1
    for x = baseX, tipX, step do
        local f = (x - baseX) / width
        local hh = halfHeight * (1 - f)
        for y = math.floor(cy - hh), math.ceil(cy + hh) do
            px(canvas, x, y, color)
        end
    end
end

-------------------------------
-- MINI POLICE PIXEL 5x7
-------------------------------

local FONT = {
    R = { "####.", "#...#", "#...#", "####.", "#..#.", "#...#", "#...#" },
    T = { "#####", "..#..", "..#..", "..#..", "..#..", "..#..", "..#.." },
}

local function drawGlyph(canvas, pattern, x0, y0, scale, color)
    for row = 1, #pattern do
        local line = pattern[row]
        for col = 1, #line do
            if line:sub(col, col) == "#" then
                for sy = 0, scale - 1 do
                    for sx = 0, scale - 1 do
                        px(canvas, x0 + (col - 1) * scale + sx, y0 + (row - 1) * scale + sy, color)
                    end
                end
            end
        end
    end
end

local function drawText(canvas, x, y, text, fg, bg)
    for i = 1, #text do
        px(canvas, x + i - 1, y, bg, text:sub(i, i), fg)
    end
end

-------------------------------
-- SYMBOLES DE LA MACHINE A SOUS (dessins originaux)
-------------------------------

local function drawSymbolBall(canvas, cx, cy, r)
    fillEllipse(canvas, cx, cy, r + 1, r + 1, colors.gray)
    for y = math.floor(cy - r), math.ceil(cy + r) do
        local dy = (y - cy) / r
        local t = 1 - dy * dy
        if t >= 0 then
            local dx = r * math.sqrt(t)
            local color
            if y < cy - r * 0.08 then
                color = colors.black
            elseif y > cy + r * 0.08 then
                color = colors.red
            else
                color = colors.lightGray
            end
            for x = math.floor(cx - dx), math.ceil(cx + dx) do
                px(canvas, x, y, color)
            end
        end
    end
    for x = math.floor(cx - r), math.ceil(cx + r) do
        px(canvas, x, cy, colors.black)
    end
    local buttonR = math.max(1, r * 0.28)
    fillEllipse(canvas, cx, cy, buttonR, buttonR, colors.white)
    fillEllipse(canvas, cx, cy, buttonR * 0.5, buttonR * 0.5, colors.lightGray)
end

local function drawSymbolSeven(canvas, cx, cy, r)
    fillEllipse(canvas, cx, cy, r, r, colors.black)
    fillEllipse(canvas, cx, cy, r * 0.8, r * 0.8, colors.red)
    local scale = math.max(1, math.floor(r / 3))
    drawGlyph(canvas, FONT.T, math.floor(cx - 2.5 * scale), math.floor(cy - 3.5 * scale), scale, colors.yellow)
end

local function drawSymbolStar(canvas, cx, cy, r)
    fillTriangleUp(canvas, cx, cy, cy - r, r * 0.24, colors.yellow)
    fillTriangleDown(canvas, cx, cy, cy + r, r * 0.24, colors.yellow)
    fillTriangleSide(canvas, cx, cx - r, cy, r * 0.24, colors.yellow)
    fillTriangleSide(canvas, cx, cx + r, cy, r * 0.24, colors.yellow)
    fillEllipse(canvas, cx, cy, r * 0.22, r * 0.22, colors.white)
end

local function drawSymbolFlame(canvas, cx, cy, r)
    fillEllipse(canvas, cx, cy + r * 0.15, r * 0.7, r * 0.6, colors.red)
    fillTriangleUp(canvas, cx, cy + r * 0.15, cy - r, r * 0.55, colors.red)
    fillEllipse(canvas, cx, cy + r * 0.25, r * 0.45, r * 0.4, colors.orange)
    fillTriangleUp(canvas, cx, cy + r * 0.25, cy - r * 0.55, r * 0.32, colors.orange)
    fillEllipse(canvas, cx, cy + r * 0.35, r * 0.2, r * 0.2, colors.yellow)
end

local function drawSymbolCoin(canvas, cx, cy, r)
    fillEllipse(canvas, cx, cy, r, r, colors.brown)
    fillEllipse(canvas, cx, cy, r * 0.8, r * 0.8, colors.orange)
    fillEllipse(canvas, cx, cy, r * 0.55, r * 0.55, colors.yellow)
end

local function drawSymbol(canvas, id, cx, cy, r)
    if id == "BALL" then
        drawSymbolBall(canvas, cx, cy, r)
    elseif id == "SEVEN" then
        drawSymbolSeven(canvas, cx, cy, r)
    elseif id == "STAR" then
        drawSymbolStar(canvas, cx, cy, r)
    elseif id == "FLAME" then
        drawSymbolFlame(canvas, cx, cy, r)
    elseif id == "COIN" then
        drawSymbolCoin(canvas, cx, cy, r)
    end
end

-------------------------------
-- TABLE DES GAINS
-------------------------------

local SYMBOLS = {
    { id = "BALL",  weight = 1, mult = 20 },
    { id = "SEVEN", weight = 2, mult = 10 },
    { id = "STAR",  weight = 3, mult = 7 },
    { id = "FLAME", weight = 4, mult = 5 },
    { id = "COIN",  weight = 5, mult = 3 },
}

local TOTAL_WEIGHT = 0
for _, s in ipairs(SYMBOLS) do TOTAL_WEIGHT = TOTAL_WEIGHT + s.weight end

local MULT_BY_ID = {}
for _, s in ipairs(SYMBOLS) do MULT_BY_ID[s.id] = s.mult end

local function pickSymbol()
    local roll = math.random(TOTAL_WEIGHT)
    local acc = 0
    for _, s in ipairs(SYMBOLS) do
        acc = acc + s.weight
        if roll <= acc then return s.id end
    end
    return SYMBOLS[#SYMBOLS].id
end

local function evaluateWin(symbols, bet)
    if symbols[1] == symbols[2] and symbols[2] == symbols[3] then
        local mult = MULT_BY_ID[symbols[1]] or 1
        return bet * mult, "JACKPOT ! 3x " .. symbols[1] .. " (x" .. mult .. ")"
    elseif symbols[1] == symbols[2] or symbols[2] == symbols[3] or symbols[1] == symbols[3] then
        return bet, "Petit gain, mise remboursee !"
    else
        return 0, "Perdu... retente ta chance !"
    end
end

-------------------------------
-- ECRAN DE JEU (compteurs LCD, fenetres de rouleaux, badges,
-- indicateurs de rotation) - style Team Rocket original
-------------------------------

local ROW_KEYS = { "top", "mid", "bot" }

local function computeLayout()
    local badgeW = math.max(3, math.floor(W * 0.07))
    local badgeGap = math.max(1, math.floor(W * 0.02))
    local reelGap = math.max(1, math.floor(W * 0.02))

    local usable = W - 2 * badgeW - 2 * badgeGap - 2 * reelGap - 4
    local reelPanelW = math.max(5, math.floor(usable / 3))
    local reelsTotalW = reelPanelW * 3 + reelGap * 2
    local contentW = badgeW * 2 + badgeGap * 2 + reelsTotalW

    local startX = math.max(2, math.floor((W - contentW) / 2) + 1)
    local leftBadgeX = startX
    local reelsX = leftBadgeX + badgeW + badgeGap
    local rightBadgeX = reelsX + reelsTotalW + badgeGap

    return {
        leftBadgeX = leftBadgeX,
        rightBadgeX = rightBadgeX,
        reelsX = reelsX,
        badgeW = badgeW,
        reelPanelW = reelPanelW,
        reelGap = reelGap,
    }
end

local function drawGameScreen(player, credit, lastGain, bet, reels, message, lockedCount)
    local canvas = newCanvas(colors.black)

    for x = 1, W do
        px(canvas, x, 1, colors.red)
        px(canvas, x, H, colors.red)
    end
    for y = 1, H do
        px(canvas, 1, y, colors.red)
        px(canvas, W, y, colors.red)
    end

    fillTriangleUp(canvas, 4, H - 1, H - 6, 2, colors.orange)
    fillTriangleUp(canvas, W - 3, H - 1, H - 6, 2, colors.orange)

    local buttons = {}
    local btnY = 2
    local cursor = 3
    local function btn(id, label, bg, fg)
        local text = " " .. label .. " "
        for i = 1, #text do
            px(canvas, cursor + i - 1, btnY, bg, text:sub(i, i), fg or colors.white)
        end
        table.insert(buttons, { id = id, x1 = cursor, x2 = cursor + #text - 1, y1 = btnY, y2 = btnY })
        cursor = cursor + #text + 1
    end
    btn("BET_DOWN", "-MISE", colors.red)
    btn("BET_UP", "+MISE", colors.green)
    btn("SPIN", "TOURNER x" .. bet, colors.orange, colors.black)
    btn("QUIT", "QUITTER", colors.gray)

    local lcdY1, lcdY2 = 4, 6
    local lcdW = math.max(8, math.floor(W * 0.26))
    local pairGap = math.max(2, math.floor(W * 0.03))
    local pairW = lcdW * 2 + pairGap
    local leftLcdX1 = math.floor((W - pairW) / 2) + 1
    local leftLcdX2 = leftLcdX1 + lcdW
    local rightLcdX1 = leftLcdX2 + pairGap
    local rightLcdX2 = rightLcdX1 + lcdW

    fillRect(canvas, leftLcdX1, lcdY1, leftLcdX2, lcdY2, colors.black)
    fillRect(canvas, rightLcdX1, lcdY1, rightLcdX2, lcdY2, colors.black)
    drawText(canvas, leftLcdX1 + 1, lcdY1, "CREDIT", colors.gray, colors.black)
    drawText(canvas, rightLcdX1 + 1, lcdY1, "GAIN", colors.gray, colors.black)
    drawText(canvas, leftLcdX1 + 1, lcdY1 + 1, tostring(credit), colors.lime, colors.black)
    drawText(canvas, rightLcdX1 + 1, lcdY1 + 1, tostring(lastGain), colors.lime, colors.black)

    local layout = computeLayout()
    local reelsTopY = 9
    local reelH = math.max(9, math.floor(H * 0.4))
    local reelsBottomY = reelsTopY + reelH

    local rowY = {
        reelsTopY + math.floor(reelH / 6),
        reelsTopY + math.floor(reelH / 2),
        reelsTopY + math.floor(reelH * 5 / 6),
    }

    local badgeVals = { "2", "3", "2" }
    local badgeColors = { colors.orange, colors.red, colors.orange }
    for i = 1, 3 do
        local halfW = math.max(1, math.floor(layout.badgeW / 2))
        fillEllipse(canvas, layout.leftBadgeX + halfW, rowY[i], halfW, 1, badgeColors[i])
        fillEllipse(canvas, layout.rightBadgeX + halfW, rowY[i], halfW, 1, badgeColors[i])
        drawText(canvas, layout.leftBadgeX + halfW, rowY[i], badgeVals[i], colors.black, badgeColors[i])
        drawText(canvas, layout.rightBadgeX + halfW, rowY[i], badgeVals[i], colors.black, badgeColors[i])
    end

    for c = 1, 3 do
        local x1 = layout.reelsX + (c - 1) * (layout.reelPanelW + layout.reelGap)
        local x2 = x1 + layout.reelPanelW
        fillRect(canvas, x1 - 1, reelsTopY - 1, x2 + 1, reelsBottomY + 1, colors.black)
        fillRect(canvas, x1, reelsTopY, x2, reelsBottomY, colors.lightGray)

        for r = 1, 3 do
            local id = reels[c][ROW_KEYS[r]]
            local cx = math.floor((x1 + x2) / 2)
            local cy = rowY[r]
            local symR = math.max(1, math.floor(math.min(layout.reelPanelW, reelH / 3) * 0.38))
            drawSymbol(canvas, id, cx, cy, symR)
        end

        local paylineTopY = rowY[2] - math.floor(reelH / 6) + 1
        local paylineBotY = rowY[2] + math.floor(reelH / 6) - 1
        for x = x1, x2 do
            px(canvas, x, paylineTopY, colors.red)
            px(canvas, x, paylineBotY, colors.red)
        end
    end

    local circleY = reelsBottomY + 3
    for c = 1, 3 do
        local x1 = layout.reelsX + (c - 1) * (layout.reelPanelW + layout.reelGap)
        local x2 = x1 + layout.reelPanelW
        local cx = math.floor((x1 + x2) / 2)
        local locked = lockedCount and (c <= lockedCount)
        fillEllipse(canvas, cx, circleY, 1.4, 1.4, colors.black)
        fillEllipse(canvas, cx, circleY, 1, 1, locked and colors.yellow or colors.gray)
    end

    local msgY = circleY + 3
    local msgX = math.max(2, math.floor((W - #message) / 2) + 1)
    drawText(canvas, msgX, msgY, message, colors.yellow, colors.black)

    render(canvas)
    return buttons
end

local function spinAnimation(player, creditDuringSpin, bet)
    local reels = {}
    for i = 1, 3 do
        reels[i] = { top = pickSymbol(), mid = pickSymbol(), bot = pickSymbol() }
    end
    local stopTicks = { 10, 16, 22 }
    local maxTick = stopTicks[3]

    for tick = 1, maxTick do
        local lockedCount = 0
        for i, reel in ipairs(reels) do
            if tick <= stopTicks[i] then
                reel.top = reel.mid
                reel.mid = reel.bot
                reel.bot = pickSymbol()
            else
                lockedCount = lockedCount + 1
            end
        end
        drawGameScreen(player, creditDuringSpin, "...", bet, reels, "Ca tourne...", lockedCount)
        sleep(0.06)
    end

    return reels
end

local function runGameSession(player)
    local credit = bank.getBalance(player)
    local bet = BET_MIN
    local lastGain = 0
    local message = "Place ta mise et lance les rouleaux !"
    local reels = {
        { top = "COIN", mid = "COIN", bot = "COIN" },
        { top = "COIN", mid = "COIN", bot = "COIN" },
        { top = "COIN", mid = "COIN", bot = "COIN" },
    }
    local playing = true

    while playing do
        credit = bank.getBalance(player)
        local buttons = drawGameScreen(player, credit, lastGain, bet, reels, message, nil)

        local _, _, x, y = os.pullEvent("mouse_click")
        local pressed = nil
        for _, b in ipairs(buttons) do
            if x >= b.x1 and x <= b.x2 and y >= b.y1 and y <= b.y2 then
                pressed = b.id
                break
            end
        end

        if pressed == "BET_DOWN" then
            bet = math.max(BET_MIN, bet - BET_STEP)
        elseif pressed == "BET_UP" then
            bet = math.min(BET_MAX, bet + BET_STEP)
        elseif pressed == "SPIN" then
            if credit < bet then
                message = "Solde insuffisant !"
            else
                local newBal, err = bank.addBalance(player, -bet)
                if not newBal then
                    message = err or "Erreur banque, reessaie."
                else
                    reels = spinAnimation(player, newBal, bet)
                    local gain, resultMsg = evaluateWin({ reels[1].mid, reels[2].mid, reels[3].mid }, bet)
                    if gain > 0 then
                        bank.addBalance(player, gain)
                    end
                    lastGain = gain
                    message = resultMsg
                    credit = bank.getBalance(player)
                end
            end
        elseif pressed == "QUIT" then
            playing = false
        end
    end
end

-------------------------------
-- CONNEXION AU CLAVIER PHYSIQUE (texte simple, avant le visuel)
-------------------------------

local function centerTextLine(y, text, fg)
    term.setBackgroundColor(colors.black)
    term.setTextColor(fg or colors.white)
    local x = math.max(1, math.floor((W - #text) / 2) + 1)
    term.setCursorPos(x, y)
    term.write(text)
end

local function authenticateKeyboard()
    term.setBackgroundColor(colors.black)
    term.clear()
    centerTextLine(2, "=== ROCKET CASINO ===", colors.red)
    term.setCursorPos(1, 4)
    term.setTextColor(colors.white)
    term.write("Pseudo Minecraft : ")
    local username = read()

    while true do
        term.setTextColor(colors.white)
        term.write("Mot de passe : ")
        local password = read("*")
        local ok, err = bank.authenticate(username, password)
        if ok then
            return username
        else
            term.setTextColor(colors.red)
            print(err or "Erreur de connexion, reessaie.")
        end
    end
end

local function loginThenPlayLoop()
    while true do
        local player = authenticateKeyboard()
        runGameSession(player)
    end
end

-------------------------------
-- LANCEMENT : Poke Ball deco sur le moniteur + jeu sur le computer
-------------------------------

local bannerLoop = buildBannerLoop()

if bannerLoop then
    parallel.waitForAll(bannerLoop, loginThenPlayLoop)
else
    loginThenPlayLoop()
end